#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : jarry

import sys
import os
import logging
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    # import pymel.core as pm
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class UnKnownPlugin(CheckBase):
    def __init__(self):
        super(UnKnownPlugin, self).__init__()
        pass

    def unknown_plugin(self, fix=False):
        unknown_plugins = cmds.unknownPlugin(q=True, list=True) or []
        # print unknown_plugins
        if unknown_plugins and fix:
            for up in unknown_plugins:
                cmds.unknownPlugin(up, remove=True)
        return unknown_plugins

    def unknown_node(self, fix=False):
        unknown_nodes = cmds.ls(type='unknown') or []
        if unknown_nodes and fix:
            for node in unknown_nodes:
                cmds.lockNode(node, l=False)
                cmds.delete(node)
        return unknown_nodes

    def check(self):
        unknown_plugin = self.unknown_plugin()
        if unknown_plugin:
            result_log = u'Error:%s\n' % unknown_plugin
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        result_proc = self.unknown_plugin()
        if result_proc:
            try:
                self.unknown_plugin(fix=True)
                result_log = u'Succeed:%s已经删除\n' % result_proc
                log.info(result_log)
                return result_log
            except:
                return u'Error:%s没有删除成功\n' % result_proc

        else:
            return u'Succeed:没有问题'
